﻿using System;
using System.Runtime.InteropServices;

namespace working
{
    /*
  *1. 删除文件的时候确认操作：SHFILEOPSTRUCT里面的成员fFlags默认情况下会有确认操作的提示框，如果您不想要提示框，可以 shf.fFlags =  FOF_NOCONFIRMATION;

  *2. 删除到回收站和永久删除：shf.fFlags =  FOF_ALLOWUNDO，顾名思义，允许撤销的话将删除到回收站，否则将永久删除。

  *3. 操作文件或者文件夹的时候显示进度条：shf.fFlags =  FOF_SIMPLEPROGRESS

  *4. 对于要删除的文件或者文件夹使用“\0\0”结束，即"D:\\New Text Document.txt\0\0"

  *5. 如果要操作多个文件或者文件夹 ，不同的路径用“\0”间隔，即"D:\\New Text Document.txt\0D:\\New Folder\0\0"

  */
    public static class FileEx
    {
        [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto, Pack = 1)]

        public struct SHFILEOPSTRUCT
        {

            public IntPtr hwnd;

            [MarshalAs(UnmanagedType.U4)]
            public int wFunc;

            public string pFrom;

            public string pTo;

            public short fFlags;

            [MarshalAs(UnmanagedType.Bool)]
            public bool fAnyOperationsAborted;

            public IntPtr hNameMappings;

            public string lpszProgressTitle;

        }

        [DllImport("shell32.dll", CharSet = CharSet.Auto)]

        static extern int SHFileOperation(ref SHFILEOPSTRUCT FileOp);

        const int FO_RENAME = 4;

        const int FO_DELETE = 3;

        const int FO_COPY = 2;

        const int FO_MOVE = 1;

        const int FOF_ALLOWUNDO = 0x40;

        const int FOF_NOCONFIRMATION = 0x10;     //Don't     prompt     the     user.;       

        const int FOF_SIMPLEPROGRESS = 0x100;

        public static void SendToRecyclyBin(string path,bool prompt = true)
        {

            SHFILEOPSTRUCT shf = new SHFILEOPSTRUCT();

            shf.wFunc = FO_DELETE;

            shf.fFlags = (short)((prompt ? FOF_ALLOWUNDO : FOF_NOCONFIRMATION) | FOF_SIMPLEPROGRESS);

            shf.pFrom = path;

            //返回0表示操作成功
            SHFileOperation(ref shf);

        }

        public static bool Copy(string from, string to,bool prompt = true)
        {
            SHFILEOPSTRUCT shf = new SHFILEOPSTRUCT();

            shf.wFunc = FO_COPY;

            shf.fFlags = (short)((prompt ? FOF_ALLOWUNDO: FOF_NOCONFIRMATION) | FOF_SIMPLEPROGRESS);

            shf.pFrom = from;

            shf.pTo = to;

            return SHFileOperation(ref shf) == 0;

        }

        public static bool Move(string from, string to,bool prompt = true)
        {

            SHFILEOPSTRUCT shf = new SHFILEOPSTRUCT();
            shf.wFunc = FO_MOVE;

            shf.fFlags = (short)((prompt ? FOF_ALLOWUNDO : FOF_NOCONFIRMATION)| FOF_SIMPLEPROGRESS);

            shf.pFrom = from;

            shf.pTo = to;

            return SHFileOperation(ref shf) == 0;

        }

        public static bool RENAME(string from, string to, bool prompt = true)
        {

            SHFILEOPSTRUCT shf = new SHFILEOPSTRUCT();

            shf.wFunc = FO_RENAME;

            shf.fFlags = FOF_ALLOWUNDO | FOF_SIMPLEPROGRESS;

            shf.pFrom = from;

            shf.pTo = to;
            return SHFileOperation(ref shf) == 0;

        }
    }
}
